/**
 * Created by lyf on 2017/6/20.
 */

'use strict'

let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = function deployMysql(mysql) {
    return new Promise (async function(resolve,reject){
        console.log(mysql);
        for ( let i = 0 ; i < mysql.length ; i++){
            await ssh.connect({
                host: mysql[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });
            const result = await ssh.execCommand(`docker run -d --restart=on-failure:5 ` +
                ` --name mysql-master ` +
                ` -p 3306:3306 ` +
                ` -e "MYSQL_ROOT_PASSWORD=ab-mysql" ` +
                ` -e "MYSQL_DATABASE=seckill" ` +
                ` -e "REPL_USER=repl" ` +
                ` -e "REPL_HOST=%" ` +
                ` -e "REPL_PWD=ab-mysql" ` +
                ` -v ~/seckill/mysql/master-data:/var/lib/mysql ` +
                ` -v ~/seckill/mysql/master-logs:/var/log ` +
                ` foreveross/mysql-master --server_id=1 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci`
            );
            ssh.dispose();
            //result.stdout ? resolve() : reject ();

            console.log(`mysql:${mysql[i][1]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");
            resolve();

        }
    })

}


